package com.huas.controller;

import com.huas.common.PageResult;
import com.huas.common.Result;
import com.huas.entity.Article;
import com.huas.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文章管理
 */
@RestController
@Slf4j
@RequestMapping("/articleManagement")
public class ArticleController {
    @Resource
    ArticleService articleService;

    /**
     * 后端展示
     * 条件分页文章列表查询
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('articleManagement:select')")
    public Result<PageResult> page(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10")Integer pageSize,
                                   String title,String category){
        log.info("文章列表查询参数：{},{},{},{}",page,pageSize,title,category);
        PageResult pageResult = articleService.page(page,pageSize,title,category);
        return Result.success(pageResult);
    }
    /**
     * 前端展示
     * 文章列表查询
     */
    @GetMapping("/user")
    @PreAuthorize("hasAuthority('articleManagement:user:select')")
    public Result<List<Article>> selectArticleList(){
        log.info("前端展示文章列表集合");
        List<Article> articleList = articleService.selectArticleList();
        return Result.success(articleList);
    }
    /**
     * 前端展示
     * 用户查询文章详情
     */
    @GetMapping("/user/{id}")
    @PreAuthorize("hasAuthority('articleManagement:user:select')")
    public Result<Article> selectArticle(@PathVariable("id") Integer id){
        log.info("用户查询文章详情,文章id为：{}",id);
        Article article = articleService.selectArticle(id);
        return Result.success(article);
    }
    /**
     * 添加文章
     */
    @PostMapping
    @PreAuthorize("hasAuthority('articleManagement:insert')")
    public Result insert(@RequestBody Article article){
        log.info("添加的文章信息为：{}", article);
        articleService.insert(article);
        return Result.success();
    }
    /**
     * 修改文章
     */
    @PutMapping
    @PreAuthorize("hasAuthority('articleManagement:update')")
    public Result update(@RequestBody Article article){
        log.info("要修改的文章信息为：{}", article);
        articleService.update(article);
        return Result.success();
    }
    /**
     * 删除文章
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('articleManagement:delete')")
    public Result delete(@PathVariable Integer id){
        log.info("要删除的文章id为：{}",id);
        articleService.delete(id);
        return Result.success();
    }
    /**
     * 批量删除文章
     */
    @DeleteMapping()
    @PreAuthorize("hasAuthority('articleManagement:delete')")
    public Result deleteBatch(@RequestParam("ids") List<Integer> ids){
        log.info("要批量删除的文章id为：{}",ids);
        articleService.deleteBatch(ids);
        return Result.success();
    }
    /**
     * 查询文章分类
     */
    @GetMapping("/category")
    @PreAuthorize("hasAuthority('articleManagement:select')")
    public Result<List<String>> selectArticleCategory(){
        log.info("查询文章分类");
        List<String> categoryList  = articleService.selectArticleCategory();
        return Result.success(categoryList);
    }

}
